package model

import (
	"strconv"
	"strings"
)

type AdminUser struct {
	BaseModel
	Username string `gorm:"not null;default:''" json:"username"`
	Password string `gorm:"not null;default:''"`
	Status   bool   `gorm:"comment:状态1启用0禁用" json:"status"`
	Roles    string `gorm:"not null;default:''"`
	LastIp   string `gorm:"size:35;not null;default:''" json:"last_ip"`
	LastTime uint   `gorm:"not null;default:0" json:"last_time"`
}

func AuthCheck(path string, roles string) bool {

	//验证path是否添加菜单 未添加菜单默认放行
	var menu AdminMenu
	ModelDb.Where("href = ?", path).First(&menu)

	if menu.ID == 0 {
		return true
	}

	var roleslist []AdminRole

	ModelDb.Where("id in ?", strings.Split(roles, ",")).Find(&roleslist)

	var roleids = make(map[string]struct{})

	for _, v := range roleslist {

		rules := strings.Split(v.Rules, ",")

		for _, r := range rules {
			roleids[r] = struct{}{}
		}
	}

	//判断是否是超级管理员
	_, ok := roleids["*"]
	if ok {
		return true
	}

	_, ok = roleids[strconv.FormatUint(uint64(menu.ID), 10)]

	return ok
	// if ok {
	// 	return true
	// }

	// return false
}
